/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.acq.service.fammer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.acq.entity.breedinfo.AcqBreedInfo;
import com.thinkgem.jeesite.modules.acq.entity.dxdan.AcqDxdan;
import com.thinkgem.jeesite.modules.acq.entity.fammer.AcqFarmer;
import com.thinkgem.jeesite.modules.acq.entity.plantinfo.AcqPlantInfo;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.acq.dao.breedinfo.AcqBreedInfoDao;
import com.thinkgem.jeesite.modules.acq.dao.dxdan.AcqDxdanDao;
import com.thinkgem.jeesite.modules.acq.dao.fammer.AcqFarmerDao;
import com.thinkgem.jeesite.modules.acq.dao.plantinfo.AcqPlantInfoDao;

/**
 * 农户信息Service
 * @author lj
 * @version 2018-04-12
 */
@Service
@Transactional(readOnly = true)
public class AcqFarmerService extends CrudService<AcqFarmerDao, AcqFarmer> {
	@Autowired AcqPlantInfoDao plantDao;
	@Autowired AcqBreedInfoDao breedDao;
	@Autowired AcqDxdanDao dxdDao;
	
	public AcqFarmer get(String id) {
		return super.get(id);
	}
	
	public List<AcqFarmer> findList(AcqFarmer acqFarmer) {
		return super.findList(acqFarmer);
	}
	
	public Page<AcqFarmer> findPage(Page<AcqFarmer> page, AcqFarmer acqFarmer) {
		return super.findPage(page, acqFarmer);
	}
	
	@Transactional(readOnly = false)
	public void save(AcqFarmer acqFarmer) {
		super.save(acqFarmer);
	}

	
	@Transactional(readOnly = false)
	public void delete(AcqFarmer acqFarmer) {
		super.delete(acqFarmer);
		dao.deletePlant(acqFarmer);
		dao.deleteBreed(acqFarmer);
	}
	
	@Transactional(readOnly = false)
	public Map<String,Object> fammers(AcqFarmer acqFarmer) {
		Map<String,Object> result = new HashMap<String, Object>();
		try{
			result.put("data", dao.fammers(acqFarmer));
			result.put("msg", "成功");
			result.put("success", true);
			return result;
		}catch(Exception e){
			result.put("msg", "失败");
			result.put("success", false);
			e.printStackTrace();
			return result;
		}
		
	}
	
	@Transactional(readOnly = false)
	public Map<String,Object> fammerjson(AcqFarmer acqFarmer) {
		Map<String,Object> result = new HashMap<String, Object>();
		try{
			result.put("data", dao.fammerjson(acqFarmer));
			result.put("msg", "成功");
			result.put("success", true);
			return result;
		}catch(Exception e){
			result.put("msg", "失败");
			result.put("success", false);
			e.printStackTrace();
			return result;
		}
		
	}
	
	@Transactional(readOnly = false)
	public Map<String,Object> saveFammer(AcqFarmer acqFarmer) {
		Map<String,Object> result = new HashMap<String, Object>();
		try{
			acqFarmer.setId(IdGen.uuid());
			//验证身份证是否重复
			Integer countIdCard = dao.checkIdCard(acqFarmer);
			if(countIdCard>0){
				result.put("msg", "录入失败,已存在相同身份证号");
				result.put("success", false);
				return result;
			}
			dao.insert(acqFarmer);
			result.put("msg", "农户信息保存成功");
			result.put("success", true);
			return result;
		}catch(Exception e){
			result.put("msg", "网络异常，请刷新后再试");
			result.put("success", false);
			e.printStackTrace();
			return result;
		}

		
	}
	@Transactional(readOnly = false)
	public Map<String,Object> history(AcqFarmer acqFarmer) {
		Map<String,Object> result = new HashMap<String, Object>();
		try{
			//1.养殖列表
			AcqBreedInfo breedparam = new AcqBreedInfo();
			breedparam.setAcqFarmerId(acqFarmer.getId());
			result.put("breedList",breedDao.breedList(breedparam));
			//2.种植列表
			AcqPlantInfo plantparam = new AcqPlantInfo();
			plantparam.setAcqFarmerId(acqFarmer.getId());

			result.put("plantList", plantDao.plantList(plantparam));
			//3.订销单列表
			AcqDxdan dxdanparam = new AcqDxdan();
			dxdanparam.setFarmerId(acqFarmer.getId());
			result.put("dxdanList",dxdDao.dxdanList(dxdanparam));			
			result.put("msg", "接口调用成功");
			result.put("success", true);
			return result;
		}catch(Exception e){
			result.put("msg", "网络异常，请稍后再试");
			result.put("success", false);
			e.printStackTrace();
			return result;
		}
		
		
	}
	@Transactional(readOnly = false)
	public Map<String,Object> infojson(AcqFarmer acqFarmer) {
		Map<String,Object> result = new HashMap<String, Object>();
		try{
		
			result.put("data",dao.infojson(acqFarmer));			
			result.put("msg", "接口调用成功");
			result.put("success", true);
			return result;
		}catch(Exception e){
			result.put("msg", "网络异常，请稍后再试");
			result.put("success", false);
			e.printStackTrace();
			return result;
		}
		
		
	}
	@Transactional(readOnly = false)
	public List<Map<String,Object>> farmerpie(String areaId) {
		return dao.pie(areaId);
	}
	@Transactional(readOnly = false)
	public Map<String,Object> farmerline(String areaId) {
		Map<String,Object> result = new HashMap<String,Object>();
		List<String> city = new ArrayList<String>();
		List<Long> data = new ArrayList<Long>();
		List<Map<String,Object>> list = dao.line(areaId);
		for(Map<String,Object> map :list){
			city.add((String)map.get("x"));
			data.add((Long)map.get("data"));
		}
		result.put("city",city);
		result.put("data",data);
		return result;
	}
	@Transactional(readOnly = false)
	public Map<String,Object> historySeacher(User user) {
		Map<String,Object> result = new HashMap<String,Object>();
		String id = user.getId();
		List<Map<String,Object>> plant = plantDao.historySeacher(id);
		List<Map<String,Object>> breed = breedDao.historySeacher(id);
		result.put("plant", plant);
		result.put("breed", breed);
		return result;
	}
	@Transactional(readOnly = false)
	public Map<String,Object> deleteHistory(String id) {
		Map<String,Object> result = new HashMap<String,Object>();
		try{
			plantDao.deleteHistory(id);
			breedDao.deleteHistory(id);
			result.put("success", true);
			result.put("msg", "删除成功");
			return result;
		}catch(Exception e){
			
			result.put("success", false);
			result.put("msg", "删除失败");
			return result;
		}
	}
	@Transactional(readOnly = false)	
	public Map<String,Object> saveBreed(AcqBreedInfo breed){
		Map<String,Object> result = new HashMap<String,Object>();
		try{
			breedDao.updateBreed(breed);
			result.put("success",true);
			result.put("msg","修改成功");
			return result;	
		}catch(Exception e){
			result.put("success", false);
			result.put("success", "修改失败");
			return result;	
		}
		
	}
	
	@Transactional(readOnly = false)	
	public Map<String,Object> savePlant(AcqPlantInfo plant){
		Map<String,Object> result = new HashMap<String,Object>();
		try{
			plantDao.updatePlant(plant);
			result.put("success",true);
			result.put("msg","修改成功");
			return result;	
		}catch(Exception e){
			result.put("success", false);
			result.put("success", "修改失败");
			return result;	
		}
		
	}
	
}